﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GRC_TPE_WebApp.Controller;

namespace GRC_TPE_WebApp.View.Private.Client
{
    public partial class ListContacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Update_button_Click(object sender, EventArgs e)
        {
            String test = (String)((Button)sender).CommandArgument;
            Response.Redirect("EditContact.aspx?id=" + test);
        }
        protected void B_Filter_Click(object sender, EventArgs e)
        {
            bool filtered = false;

            ///suppression de tous les paramètres existants
            this.DS_Contacts.WhereParameters.Clear();

            ///ajout du filtre sur le nom
            if (!string.IsNullOrEmpty(this.TB_FilterNom.Text))
            {
                this.DS_Contacts.Where += "it.LastName LIKE '%" + this.TB_FilterNom.Text + "%'";
                filtered = true;
            }

            ///ajout du filtre sur la fonction
            if (!string.IsNullOrEmpty(this.TB_FilterFonction.Text))
            {
                if (filtered)
                    this.DS_Contacts.Where += " AND ";
                this.DS_Contacts.Where += "it.Function LIKE '%" + this.TB_FilterFonction.Text + "%'";
                filtered = true;
            }

            ///ajout du filtre sur le client
            if (!string.IsNullOrEmpty(this.TB_FilterClient.Text))
            {
                if (filtered)
                    this.DS_Contacts.Where += " AND ";
                this.DS_Contacts.Where += "it.Client.Name LIKE '%" + this.TB_FilterClient.Text + "%'";
            }

            string test = this.DS_Contacts.Where;
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de réinitialisation de filtres
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Clear_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListContacts.aspx");
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de suppression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Delete_Click(object sender, EventArgs e)
        {
            String ids = (String)((Button)sender).CommandArgument;
            int id = int.Parse(ids);
            if (ContactController.DeleteContact(id) == "No error")
                Response.Redirect("ListContacts.aspx");
        }
    }
}